package com.qububu.emporium.orderform.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qububu.db.dao.OrderFormInfoDao;
import com.qububu.emporium.orderform.dto.OrderFormInfoDto;
import com.qububu.emporium.orderform.service.OrderFormService;
import com.qububu.emporium.shoppingCart.dto.CartSaveGoodsMessageDto;
import com.qububu.utils.DateUtil;
import com.qububu.utils.ParamConverter;

/**
 * @功能 订单业务逻辑的处理
 * @详情 
 * */
@Service
public class OrderFormServiceImpl implements OrderFormService {
	
	private static Map<String, String> cityMap;
	private static Map<String, String> addressMap;
	
	static {
		cityMap = new HashMap<String, String>();
		cityMap.put("成都", "CD");
	}
	static {
		addressMap = new HashMap<String, String>();
		addressMap.put("泰和佳园", "01");
	}
	
	@Resource
	private OrderFormInfoDao orderFormInfoDao;
	/*注意, orderFormInfoDao这个名字和配置文件中的
	 * <bean id="orderFormInfoDao" class="com.qububu.db.dao.impl.OrderFormInfoDaoImpl">
	 *  <property name="dataSource" ref="dataSource"></property>
	 * </bean>
	 * id名必须一致
	 * */
	
	public List<OrderFormInfoDto> getOrderFormListByOrderStatus(int status) {
		return orderFormInfoDao.getOrderFormListByOrderStatus(status);
	}

	public boolean doAddOrderForm(OrderFormInfoDto orderFormInfoDto, Map<String, CartSaveGoodsMessageDto> goodsMap) {
		
		List<CartSaveGoodsMessageDto> goodsList = new ArrayList<CartSaveGoodsMessageDto>();
		double totalPrice = 0;
		for (Entry<String, CartSaveGoodsMessageDto> entry : goodsMap.entrySet()) {
			CartSaveGoodsMessageDto goods = entry.getValue();
			totalPrice += Double.parseDouble(goods.getGoodsPrice()) * goods.getGoodsQuantity();
			goodsList.add(goods);
		}
		if (totalPrice == orderFormInfoDto.getOrderAmount()) {
			orderFormInfoDao.doAddOrderForm(orderFormInfoDto, goodsList);
			return true;
		}
		return false;
	}

	public String genOrderFormNumber() {
		String city = ParamConverter.assertNotEmpty(cityMap.get("成都"));
		String address = ParamConverter.assertNotEmpty(addressMap.get("泰和佳园"));
		
		int nowTime = DateUtil.convertCurrentDTTMtoInt();
		Date dateNowTime = DateUtil.convertIntToDTTM(nowTime);
		String time = String.valueOf(DateUtil.getDateMillisecond(dateNowTime));

		/*if (ParamConverter.convertToInteger(userId) < 10) {
			userId = "000" + userId;
		} else if (ParamConverter.convertToInteger(userId) < 100) {
			userId = "00" + userId;
		} else if (ParamConverter.convertToInteger(userId) < 1000) {
			userId = "0" + userId;
		}*/
		
		String orderNo = city+address+time;
		return orderNo;
	}

	public List<CartSaveGoodsMessageDto> getGoodsListByOrderNo(String orderNo) {
		return orderFormInfoDao.getGoodsListByOrderNo(orderNo);
	}

	public void doChangeOrderStatus(String orderId, int status) {
		orderFormInfoDao.doChangeOrderStatus(orderId, status);
	}

	
	
	public List<OrderFormInfoDto> getQueryOrderListByUserId(String userId) {
		return orderFormInfoDao.getQueryOrderListById(userId);
	}

	public OrderFormInfoDto getQueryOrderInfoDetalByOrderNo(String orderNo) {
		return orderFormInfoDao.getQueryOrderInfoDetalByOrderNo(orderNo);
	}

	public void doDeleteOrderFormByOrderNo(String orderNo) {
		orderFormInfoDao.doDeleteOrderForm(orderNo);
	}

}
